package com.cspg.design.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.utils.StatusBarUtil;

public class SettingFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        SettingViewModel settingViewModel = ViewModelProviders.of(this).get(SettingViewModel.class);
        View root = inflater.inflate(R.layout.fragment_setting, container, false);

        StatusBarUtil.setStatusBarColor(getActivity(),R.color.home_bg_main);
        StatusBarUtil.setLightStatusBar(getActivity(),true,false);


        final TextView textView = root.findViewById(R.id.text_setting);
        settingViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        UserInfo userInfo = UserInfo.getInstance();
        String userName = userInfo.userName;
        TextView tv_setting_username = root.findViewById(R.id.tv_setting_username);
        tv_setting_username.setText(userName);
        Button btn_setting_logout = root.findViewById(R.id.btn_setting_logout);
        btn_setting_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ((CSPGApplication) getActivity().getApplication()).getDaoSession().getUserInfoBeanDao().deleteAll();
                UserInfo.setUserInfo(null);
                startActivity(new Intent(getContext(), LoginActivity.class));
                getActivity().finish();
            }
        });
        LinearLayout ll_setting_pwd = root.findViewById(R.id.ll_setting_password);
        ll_setting_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(),SecurityActivity.class));
            }
        });

        return root;
    }
}